﻿using OASystem.BLL.IService;
using OASystem.DAL;
using OASystem.Model;
using OASystem.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;

namespace OASystem.BLL.Service
{
    public class AhomeService : BaseRepository<Tasklist>, IAhomeService
    {
        BaseRepository<Userinfo> uf = new BaseRepository<Userinfo>();
        BaseRepository<Handle> han = new BaseRepository<Handle>();
        public ResponseResult Display(int ID)
        {
            ResponseResult result = new ResponseResult();
            Userinfo us = new Userinfo();
            List<Tasklist> user = QueryList(a => a.oneer == ID);
            List<Taskquery> user2 = new List<Taskquery>();

            for (int i = user.Count - 1; i >= 0; i--)
            {
                Taskquery tq = new Taskquery();
                int Id = Convert.ToInt32(user[i].Publisher);
                us = uf.Query(a => a.UserId == Id);
                tq.Publisher = us.EmployeeName;
                tq.CTime = Convert.ToString(user[i].CompletionTime);
                tq.TaskTiltle = user[i].TaskTiltle;
                user2.Add(tq);
            }
            result.data = user2;
            return result;
        }

        public ResponseResult Select(int Id, int term)
        {
            ResponseResult result = new ResponseResult();
            List<Handle> handles = new List<Handle>();
            List<Handle> hean = new List<Handle>();
            handles = han.QueryList(a => a.Oneer == Id);
            if (term == 1)
            {
                handles = handles.Where(a => a.Status == 1).ToList();
            }
            else
            {
                handles = handles.Where(a => a.Status == 0).ToList();
            }
            if (handles.Count > 5)
            {
                for (int i = 5 - 1; i >= 0; i--)
                {
                    hean.Add(handles[i]);
                }
            }
            else
            {
                for (int i = handles.Count - 1; i >= 0; i--)
                {
                    hean.Add(handles[i]);
                }
            }

            result.data = hean;
            return result;
        }
    }
}
